<?php
namespace jy_stteamfee;
defined('BY_JYA') or exit('error');
/*
排位
*/
class paiwei extends \plugin{
	//排位循环奖
	public function fee_commission($team=array(),$order=array()){
		if(!$team['id']){
			return false;
		}
		//说明已经轮完一圈了，就结束
		if(!$order['mid'] && $this->startpaimid && $this->startpaimid==$team['mid']){
			//循环一次
			if($this->isxuntime){
				return false;
			}
			$this->isxuntime=1;
		}
		//第一层开始排
		$this->startpaimid=$this->startpaimid?:$team['mid'];
		if($order['mid']){
			$this->orderprice=$order['price'];
			$this->ordermid=$order['mid'];
			$this->orderordersn=$order['ordersn'];
			$nowpaimid=$this->get_nowpaimid2($team);
			//file_put_contents(ROOT_D.'/2.txt',$team['mid'].'~nowpaimid~'.$nowpaimid."\n",FILE_APPEND);
			if($nowpaimid && $nowpaimid!=$team['mid']){
				$team=$this->get('team',array('mid'=>$nowpaimid,'uniacid'=>$_SESSION['uniacid']));
			}
			$this->isxuntime=0;
		}
		//file_put_contents(ROOT_D.'/2.txt',$team['mid'].'~canpai~'.$team['canpai']."\n",FILE_APPEND);
		$pailog=array(
			'mid'=>$this->ordermid,
			'ordersn'=>$this->orderordersn,
			'paimid'=>$this->startpaimid,
			'lasttomid'=>$team['paimid'],
		);
		$nowceng=$this->set_pailog($pailog);
		if(!$team['canpai']){
			if($team['paimid']){
				$nowteam=$this->get('team',array('mid'=>$team['paimid'],'uniacid'=>$_SESSION['uniacid']));
				if($nowteam && $nowteam['mid']!=$team['mid']){
					$this->fee_commission($nowteam);
				}
			}
			return false;
		}
		//file_put_contents(ROOT_D.'/2.txt',$team['mid'].'~nowceng~'.$nowceng."\n",FILE_APPEND);
		if(!$nowceng){
			if($this->startpaimid==$team['mid']){
				$nowceng=1;
			}else{
				//从新开始排
				if($this->startpaimid){
					$nowteam=$this->get('team',array('mid'=>$this->startpaimid,'uniacid'=>$_SESSION['uniacid']));
					if($nowteam && $nowteam['mid']!=$team['mid']){
						$this->fee_commission($nowteam);
					}
				}
				return false;
			}
		}
		$ceng=$team['myceng']?:$this->get_myceng($team);
		//$myorderceng=$this->get_orderceng($this->startpaimid,$team['mid']);
		//$ceng=12;
		//file_put_contents(ROOT_D.'/2.txt',$myorderceng.'~ceng~'.$ceng."\n",FILE_APPEND);
		if($ceng<$nowceng){
			//自己超出层级，就给下一层
			if($team['paimid']){
				$nowteam=$this->get('team',array('mid'=>$team['paimid'],'uniacid'=>$_SESSION['uniacid']));
				if($nowteam && $nowteam['mid']!=$team['mid']){
					$this->fee_commission($nowteam);
				}
			}
			return false;
		}
		$commission_level=$team['level_id'];
		if(!$team['level_id']){
			//获取默认等级分红比例
			$level=$this->plugin_setting('default_level');
			$level_id=0;
		}else{
			$level=$this->get('level',array('id'=>$team['level_id'],'uniacid'=>$_SESSION['uniacid']));
			$level_id=$level['id'];
		}
		$pai_per=$level['pai_per'];
		if(substr($pai_per,-1)=='%'){
			$money=intval($this->orderprice*substr($pai_per,0,-1))/100;
		}else{
			$money=$pai_per;
		}
		if($money>0){
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'merchid'=>$_SESSION['onemerchid'],
					'mid'=>$team['mid'],
					'agent_id'=>$team['fromuser'],
					'area_code'=>$team['area_code'],
					'level'=>$level_id,
					'ordersn'=>$this->orderordersn,
					'money'=>$money,
					'createtime'=>TIMESTAMP,
					'status'=>$this->setting['is_jiesuan']?:0,
					'type'=>6,
				);
				$this->insert('commission',$new_commission);
		}
		return true;
	}
	//首次获得拼团排位循环奖资格设置
	public function check_canpai($member=array()){
		$teamsetting=$this->setting;
		$team_cantype=intval($teamsetting['team_cantype']);
		$up=true;
		if($teamsetting['team_cantime']){
			$znum=pdo_count('jy_stpinggou_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid'],'type'=>3,'status'=>1));
			if($znum<$teamsetting['team_cantime']){
				$up=false;
			}
		}
		if($teamsetting['team_canfee']){
			$zfee=pdo_getsum('jy_stpinggou_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid'],'type'=>3,'status'=>1),'total_price');
			if(!$up || $team_cantype){
				if($zfee<$teamsetting['team_canfee']){
					$up=false;
				}
			}
		}
		if($up){
			pdo_update('jy_stteamfee_team',array('canpai'=>1),array('mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']));
		}
		return $up;
	}
	//获取当前循环到哪个坑位
	public function get_nowpaimid($member=array()){
		$teamsetting=$this->setting;
		$level=intval($teamsetting['team_ceng']);
		$level=$level?:6;
		$pailog=pdo_get('jy_stteamfee_pailog',array('paimid'=>$member['paimid'],'uniacid'=>$_SESSION['uniacid']));
		if($pailog['lasttomid'] && $pailog['ceng']<$level){
			$paimid=pdo_getcolumn('jy_stteamfee_team',array('mid'=>$pailog['lasttomid'],'uniacid'=>$_SESSION['uniacid']),'paimid');
		}
		if($pailog['ceng']>$level || ($pailog['id'] && !$paimid)){
			pdo_update('jy_stteamfee_pailog',array('ceng'=>0,'lasttomid'=>0),array('id'=>$pailog['id'],'uniacid'=>$_SESSION['uniacid']));
			return $pailog['paimid'];
		}
		$paimid=$paimid?:$member['paimid'];
		return $paimid;
	}
	//获取当前循环到哪个坑位2
	public function get_nowpaimid2($team=array()){
		$teamsetting=$this->setting;
		$level=intval($teamsetting['team_ceng']);
		$level=$level?:6;
		$pailog=pdo_get('jy_stteamfee_pailog',array('paimid'=>$team['mid'],'uniacid'=>$_SESSION['uniacid']));
		if($pailog['lasttomid'] && $pailog['ceng']<$level){
			$paimid=$pailog['lasttomid'];
		}
		if($pailog['ceng']>$level || ($pailog['id'] && !$paimid)){
			return $pailog['paimid'];
		}
		$paimid=$paimid?:$team['mid'];
		return $paimid;
	}
	//直推1人消费1次升级多拿1层
	public function get_myceng($team=array()){
		$teamsetting=$this->setting;
		$team_timetype=intval($teamsetting['team_timetype']);
		if($team_timetype && (!$teamsetting['team_buytime'] ||!$teamsetting['team_numtime'])){
			pdo_update('jy_stteamfee_team',array('myceng'=>1),array('mid'=>$team['mid'],'uniacid'=>$_SESSION['uniacid']));
			return 1;
		}
		if(!$team_timetype && !$teamsetting['team_buytime'] && !$teamsetting['team_numtime']){
			pdo_update('jy_stteamfee_team',array('myceng'=>1),array('mid'=>$team['mid'],'uniacid'=>$_SESSION['uniacid']));
			return 1;
		}
		$team_numtime=pdo_count('jy_stteamfee_team',array('isping'=>1,'fromuser'=>$team['mid'],'uniacid'=>$_SESSION['uniacid']));
		$team_buytime=pdo_count('jy_stpinggou_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$team['mid'],'type'=>array(0,3),'status'=>1));
		$ceng1=$teamsetting['team_numtime']>0?intval($team_numtime/$teamsetting['team_numtime']):0;
		$ceng2=$teamsetting['team_buytime']>0?intval($team_buytime/$teamsetting['team_buytime']):0;
		if($team_timetype){
			$ceng=$ceng1>$ceng2?$ceng2:$ceng1;
		}else{
			$ceng=$ceng1>$ceng2?$ceng1:$ceng2;
		}
		$ceng ++;
		pdo_update('jy_stteamfee_team',array('myceng'=>$ceng),array('mid'=>$team['mid'],'uniacid'=>$_SESSION['uniacid']));
		return $ceng;
	}
	public function get_orderceng($startmid=0,$mymid=0){
		$ceng=1;
		if($startmid!=$mymid){
			$paimid=pdo_getcolumn('jy_stteamfee_team',array('mid'=>$startmid,'uniacid'=>$_SESSION['uniacid']),'paimid');
			for($i=1;$i<100;$i++){
				$ceng++;
				if($mymid==$paimid || !$paimid){
					break;
				}else{
					$paimid=pdo_getcolumn('jy_stteamfee_team',array('mid'=>$paimid,'uniacid'=>$_SESSION['uniacid']),'paimid');
				}
			}
		}
		return $ceng;
	}
	/*
		排坑
	*/
	public function set_pailog($params){
		$teamsetting=$this->setting;
		$pailog=pdo_get('jy_stteamfee_pailog',array('paimid'=>$params['paimid'],'uniacid'=>$_SESSION['uniacid']));
		if($pailog['id']){
			$level=intval($teamsetting['team_ceng']);
			$level=$level?:6;
			if($pailog['ceng']>$level || !$params['lasttomid']){
				pdo_update('jy_stteamfee_pailog',array('ceng'=>1,'lasttomid'=>$params['lasttomid']),array('id'=>$pailog['id'],'uniacid'=>$_SESSION['uniacid']));
				return 1;
			}else{
				pdo_update('jy_stteamfee_pailog',array('ceng +='=>1,'lasttomid'=>$params['lasttomid']),array('id'=>$pailog['id'],'uniacid'=>$_SESSION['uniacid']));
				$ceng=$pailog['ceng']+1;
			}
		}else{
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$params['mid']?:$_SESSION['uid'],
				'paimid'=>$params['paimid'],
				'ordersn'=>$params['ordersn'],
				'ceng'=>1,
				'lasttomid'=>$params['lasttomid'],
				'createtime'=>TIMESTAMP
			);
			$ceng=1;
			pdo_insert('jy_stteamfee_pailog',$insert);
		}
		return $ceng;
	}
	public function set_yuelog(){
		$teamsetting=$this->setting;
		$month=date('Y-m-d');
		$yuelog=pdo_get('jy_stteamfee_yuelog',array('month'=>$month,'uniacid'=>$_SESSION['uniacid']));
		if($yuelog['id']){
			return '';
		}else{
			$yuestart = mktime(0,0,0,date('m'),1,date('Y'));
			$teams=pdo_getall('jy_stteamfee_team',array('createtime <'=>$yuestart,'uniacid'=>$_SESSION['uniacid']),array('id','mid','paimid','fromuser','canpai'));
			foreach($teams as $tv){
				$team_sf=$this->check_team_sf($tv);
				$insert=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$tv['mid'],
					'status'=>$team_sf['canpai'],
					'month'=>$month,
					'team_sftime'=>$team_sf['team_sftime'],
					'team_sffee'=>$team_sf['team_sffee'],
					'createtime'=>TIMESTAMP
				);
				pdo_insert('jy_stteamfee_yuelog',$insert);
				$update=array('canpai'=>$team_sf['canpai']);
				if(!$team_sf['canpai']){
					$this->sfpaimid($tv);
					$update['paimid']=0;
				}
				if($team_sf['canpai'] && !$tv['canpai']){
					$update['paimid']=$this->model('member')->getpaimid($tv['fromuser']);
				}
				pdo_update('jy_stteamfee_team',$update,array('id'=>$tv['id'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
		return '';
	}
	public function check_team_sf($member=array()){
		$nowyue = mktime(0,0,0,date('m'),1,date('Y'));
		$yesyue = mktime(0,0,0,date('m')-1,1,date('Y'));
		$team_sftype=intval($this->setting['team_sftype']);
		$up=true;
		if($this->setting['team_sftime']){
			$znum=pdo_count('jy_stpinggou_log',array('createtime between'=>array($yesyue,$nowyue),'uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid'],'type'=>3,'status'=>1));
			if($znum<$this->setting['team_sftime']){
				$up=false;
			}
		}
		if($this->setting['team_sffee']){
			$zfee=pdo_getsum('jy_stpinggou_log',array('createtime between'=>array($yesyue,$nowyue),'uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid'],'type'=>3,'status'=>1),'total_price');
			if(!$up || $team_sftype){
				if($zfee<$this->setting['team_sffee']){
					$up=false;
				}
			}
		}
		$team_sf=array(
			'canpai'=>$up,
			'team_sftime'=>$znum,
			'team_sffee'=>$zfee,
		);
		return $team_sf;
	}
	//释放排位，下面的顺延填坑
	public function sfpaimid($member=array()){
		for($i=0;$i<100;$i++){
			$nowmember=pdo_get('jy_stteamfee_team',array('paimid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']),array('id','mid','paimid'));
			if(!$nowmember['id']){
				break;
			}
			pdo_update('jy_stteamfee_team',array('paimid'=>$member['paimid']),array('id'=>$nowmember['id'],'uniacid'=>$_SESSION['uniacid']));
			$this->sfpaimid($nowmember);
		}
		return '';
	}
}